package com.lyc.cart.daoImpl;

import com.lyc.cart.bean.Cart;
import com.lyc.cart.bean.CartItem;
import com.lyc.cart.dao.CartDao;
import com.lyc.user.util.jdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    /**
     * 把商品添加到购物车
     * @param cart
     * @return
     */
    @Override
    public int addBookToCart(Cart cart) {
        Connection connection = null;
        PreparedStatement ps=null;
        int row=0;
        try {
            String sql="insert into shopcart (uid,productId,buyNumbers,createTime) values(?,?,?,now())";
            connection = jdbcUtil.getConnection();
            connection.setAutoCommit(false);
            ps = connection.prepareStatement(sql);
            ps.setInt(1,cart.getUid());
            ps.setInt(2,cart.getProductId());
            ps.setInt(3,cart.getBuyNumber());
            row = ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.close(null,ps,connection);
        }
        return row;
    }

    /**
     * 更新商品到购物车
     * @param cart
     * @return
     */
    @Override
    public int updateBookToCart(Cart cart) {
        Connection connection = null;
        PreparedStatement ps=null;
        int row=0;
        try {
            String sql="update shopcart set buyNumbers=buyNumbers + ? where uid = ? and productId = ? ";
            connection = jdbcUtil.getConnection();
            connection.setAutoCommit(false);
            ps = connection.prepareStatement(sql);
            ps.setInt(1,cart.getBuyNumber());
            ps.setInt(2,cart.getUid());
            ps.setInt(3,cart.getProductId());
            row = ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.close(null,ps,connection);
        }
        return row;
    }

    /**
     * 根据uid和productId查询商品是否存在
     * @param cart
     * @return
     */
    @Override
    public Cart queryBookByUidAndProductId(Cart cart) {
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet resultSet =null;
        Cart car =null;
        try {
            String sql="select id from shopcart where uid= ? and productId = ?";
            connection = jdbcUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1,cart.getUid());
            ps.setInt(2,cart.getProductId());
            resultSet = ps.executeQuery();
            if (resultSet.next()){
                car = new Cart();
                car.setUid(resultSet.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.close(resultSet,ps,connection);
        }
        return car;
    }

    /**
     * 查看添加进购物车中的商品
     * @return
     */
    @Override
    public List<CartItem> queryMyCart(int uid) {
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet resultSet =null;
        List<CartItem> cartItems=new ArrayList<CartItem>();

        try {
            String sql="SELECT p.productName,productImgurl,price,s.buyNumbers,discount,s.productId,s.uid from shopcart s,product p,usermessage u WHERE u.uid=s.uid and p.productId=s.productId and s.uid = ?";
            connection = jdbcUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1,uid);

            resultSet = ps.executeQuery();
            while (resultSet.next()){
                CartItem cartItem = new CartItem();
                cartItem.setProductName(resultSet.getString("productName"));
                cartItem.setProductImgurl(resultSet.getString("productImgurl"));
                cartItem.setPrice(resultSet.getDouble("price"));
                cartItem.setBuyNumbers(resultSet.getInt("buyNumbers"));
                cartItem.setDiscount(resultSet.getDouble("discount"));
                cartItem.setProductId(resultSet.getInt("productId"));
                cartItem.setUid(resultSet.getInt("uid"));
                cartItems.add(cartItem);
            }
            return  cartItems;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.close(resultSet,ps,connection);
        }

        return null;
    }

    /**
     * 从购物车列表里删除商品
     * @param cartItem
     * @return
     */
    @Override
    public int deleteBookToCart(CartItem cartItem) {

        Connection connection = null;
        PreparedStatement ps=null;
        int row=0;
        try {
            String sql="DELETE FROM shopcart where productId=? and uid =?; ";
            connection = jdbcUtil.getConnection();
            connection.setAutoCommit(false);
            ps = connection.prepareStatement(sql);
            ps.setInt(1,cartItem.getProductId());
            ps.setInt(2,cartItem.getUid());
            row = ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.close(null,ps,connection);
        }
        return row;
    }
}
